package test_achieve.web;

import constant.Constant;
import constant.Pm;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import util.tools.CommonTools;

import java.math.BigDecimal;

/**
 * Created by chenkuo on 2017/10/13.
 */
public class WebPrice {
    /**
     * 填写界面
     * distance-fare    路面金额
     * weight-fare      续重金额
     * night-fare       夜间服务费
     * travel-fare      交通费
     * on-floor-fare    一取多投上门费
     * premium-fare     溢价金额
     * crossRiver-fare  跨江费
     * -------------------------
     * total-fare       订单总额
     * coupon-fare      优惠券抵扣
     * final-fare       还需支付
     *************************************
     * 提交界面
     *distance-fare     路面金额
     * on-floor-fare    一取多投上门费
     * night-fare       夜间费
     *
     *--------------------------------
     *total_money       订单总额
     * must_pay         还需支付
     * use_balance      余额支付
     * addition-fare    小费
     */

    /**填写界面----------------------*/



    @Given("^w确认里程金额：(.+)$")
    public void w确认里程金额(String mon) throws Throwable {
        checkMoney(mon,"distance-fare");
    }

    @Given("^w确认一取多投上门费：(.+)$")
    public void w确认一取多投上门费(String mon) throws Throwable {
        checkMoney(mon,"on-floor-fare");
    }

    @Given("^w确认交通费：(.+)$")
    public void w确认交通费(String mon) throws Throwable {
        checkMoney(mon,"travel-fare");
    }

    @Given("^w确认跨江费：(.+)$")
    public void w确认跨江费(String mon) throws Throwable {
        checkMoney(mon,"crossRiver-fare");
    }

    @Given("^w填写界面，确认续重金额：(.+)$")
    public void w填写界面确认续重金额(String mon) throws Throwable {
        checkMoney(mon,"weight-fare");
    }

    @Given("^w填写界面，确认订单总额：(.+)$")
    public void w填写界面确认订单总额(String mon) throws Throwable {
        checkMoney(mon,"total-fare");
    }

    @Given("^w填写界面，确认优惠抵扣：(.+)$")
    public void w填写界面确认优惠抵扣(String mon) throws Throwable {
        checkMoney(mon,"coupon-fare");
    }

    @Given("^w填写界面，确认还需支付：(.+)$")
    public void w填写界面确认还需支付(String mon) throws Throwable {
        checkMoney(mon,"final-fare");
    }

    @Given("^w填写界面，货款服务费：(.+)$")
    public void w填写界面货款服务费(String mon) throws Throwable {
        String money1 = Constant.driverWeb.findElement(By.id("goodspayfee")).getText();
        String money =CommonTools.splitNumber(money1,"¥",1);
//        System.out.println("代收货款服务费："+money);
        Assert.assertEquals(money,mon);
    }

    @Given("^w填写界面，金额确认"+ Pm.p2)
    public void w填写界面金额确认(String totalFare,String couponFare) throws Throwable {
        checkMoney(totalFare,"total-fare");
        checkMoney(couponFare,"coupon-fare");
    }

    /**提交界面----------------------*/

    @Given("^w提交界面，确认里程金额：(.+)$")
    public void w提交界面确认里程金额(String mon) throws Throwable {
        checkMoney(mon,"distance-fare");
    }

    @Given("^w提交界面，一取多投上门费：(.+)$")
    public void w提交界面一取多投上门费(String mon) throws Throwable {
        checkMoney(mon,"on-floor-fare");
    }

    @Given("^w确认余额支付：(.+)$")
    public void w确认余额支付(String mon) throws Throwable {
        boolean btn = CommonTools.waitForResultById(Constant.driverWeb,mon,"use_balance");
        Assert.assertEquals(btn,true);
    }

    @Given("^w提交界面，确认订单总额：(.+)$")
    public void w提交界面确认订单总额(String mon) throws Throwable {
        boolean btn =CommonTools.waitForResultById(Constant.driverWeb,mon,"total_money");
        Assert.assertEquals(btn,true);
    }
    @Given("^w提交界面，确认优惠抵扣：(.+)$")
    public void w提交界面确认优惠抵扣(String mon) throws Throwable {
        boolean btn =CommonTools.waitForResultById(Constant.driverWeb,mon,"used_coupon");
        Assert.assertEquals(btn,true);
    }

    @Given("^w提交界面，确认还需支付：(.+)$")
    public void w提交界面还需支付(String mon) throws Throwable {
        boolean btn =CommonTools.waitForResultById(Constant.driverWeb,mon,"must_pay");
        Assert.assertEquals(btn,true);
    }

    @Given("^w提交界面，确认小费：(.+)$")
    public void w提交界面确认小费小费(String mon) throws Throwable {
        checkMoney(mon,"addition-fare");
    }
    @Given("^w提交界面，确认夜间费：(.+)$")
    public void w提交界面确认夜间费夜间费(String mon) throws Throwable {
        checkMoney(mon,"night-fare");
    }

    @Given("^w成功界面，订单总额：(.+)$")
    public void w成功界面订单总额(String mon) throws Throwable {
        CommonTools.waitElementLoad(Constant.driverWeb,"xpath","//*[@id=\"#place_order\"]/div/div/div[2]/ul[2]/li[1]/a");
        String money1 = Constant.driverWeb.findElement(By.xpath("//*[@id=\"#place_order\"]/div/div/div[2]/ul[2]/li[1]/a")).getText();
        String money = money1.substring(0,money1.indexOf("元"));
        Assert.assertEquals(money,mon);
    }



    /**账户界面、货款界面----------------------*/

    @Given("^w确认余额支付信息与优惠信息：(.+)：(.+)：(.+)$")
    public void w确认余额支付信息与优惠信息(String payMon,String amountMon,String couponMon) throws Throwable {
        String payMoney1 = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[2]")).getText();
        String payMoney = CommonTools.cutFront(payMoney1,"【");
        String money = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).getText();
//        System.out.println(payMoney1);
        String amountMoney = CommonTools.splitNumber(money,"￥",1);
        String couponMoney =CommonTools.splitNumber(money,"￥",2);
        Assert.assertEquals(payMoney,payMon);
        Assert.assertEquals(amountMoney,amountMon);
        Assert.assertEquals(Integer.parseInt(couponMoney),Math.abs(Integer.parseInt(couponMon)) );
    }


    @Given("^w确认余额支付信息：(.+)：(.+)$")
    public void w确认余额支付信息(String payMon,String amountMon) throws Throwable {
        String payMoney1 = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[2]")).getText();
        String payMoney = CommonTools.cutFront(payMoney1,"【");
        String money = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).getText();
//        System.out.println(payMoney1);
        String amountMoney = CommonTools.splitNumber(money,"￥",1);
        Assert.assertEquals(payMoney,payMon);
        Assert.assertEquals(amountMoney,amountMon);
    }

    @Given("^w点击退款记录$")
    public void w点击退款记录() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div[2]/a[3]")).click();//点击退款记录按钮
    }

    @Given("^确认退款金额与退款说明：(.+)：(.+)$")
    public void 确认退款金额与退款说明退款金额退款说明(String mon,String exp) throws Throwable {
        String money = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[2]")).getText();
        Assert.assertEquals(money,mon);
        String explain1 = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).getText();
        String explain = CommonTools.cutEnd(explain1,"账户余额】");
        Assert.assertEquals(explain,exp);
    }

    @Given("^w提交界面，货款服务费：(.+)$")
    public void w提交界面货款服务费(String mon) throws Throwable {
        String money1 = Constant.driverWeb.findElement(By.xpath("//*[@id=\"confirm_order_form\"]/div[1]/div/table/tbody/tr[3]/td")).getText();
//        System.out.println("w提交界面货款服务费"+money1);
        String money = CommonTools.splitNumber(money1,"费：¥ ",1);
//        System.out.println("w提交界面货款服务费"+money);
        Assert.assertEquals(money,mon);

    }

    /**账户结算----------------------*/

    @Given("^w确认账户余额$")
    public void 确认账户余额() throws Throwable {
        WebElement balance = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div[1]/span[2]/span[1]"));
        BigDecimal balance1 = new BigDecimal(balance.getText());//当前页面获取用户账户余额
//        System.out.println("当前页面获取的余额"+balance1);
        BigDecimal UserBalance = new BigDecimal(Constant.userBalance);//之前获取到的用户余额
        BigDecimal orderBalancePayAm= new BigDecimal(Constant.orderBalancePayAm);//订单余额支付金额
//        System.out.println("订单支付的金额："+Constant.orderBalancePayAm);
        BigDecimal expect =UserBalance.subtract(orderBalancePayAm);//用户余额=用户余额-余额支付金额
        org.junit.Assert.assertEquals(balance1, expect);
    }

    private String waitForResultByClassName(WebDriver driver, String result, String by) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            CommonTools.scrollToElement(Constant.driverWeb,By.id("price-detail-btn"));
            Constant.driverWeb.findElement(By.id("price-detail-btn")).click();
            String result1 = driver.findElement(By.className(by)).getText();
            if (result1.equals(result)){
                break;
            }
            new Actions(driver).sendKeys(Keys.DOWN).perform();
            Thread.sleep(1000);
        }
        return result;
    }

    private void checkMoney(String mon,String parameter) throws InterruptedException {
        CommonTools.scrollToElement(Constant.driverWeb,By.id("price-detail-btn"));
        Constant.driverWeb.findElement(By.id("price-detail-btn")).click();
        String btn =waitForResultByClassName(Constant.driverWeb,mon,parameter);
        Assert.assertEquals(btn,mon);
    }


    @Given("^w提交界面，确认订单总额与抵扣：(.*)：(.*)$")
    public void w提交界面确认订单总额与抵扣总额抵扣(String money,String coupon) throws Throwable {
        w提交界面确认订单总额(money);
        w提交界面确认优惠抵扣( coupon);
    }
}
